/**
 * 		MochitoTransferFactory.java
 *				------------
 *		
 *   Author:	bin04017@gmail.com
 *	 Created:	22.12.2006
 *	 Project:	Mochito-TransferFramework
 *	 Package:	MochitoTransferFactory
 *	 Usage:		factory for connection in mochito system
 * *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 */
package mochitoTransferFactory;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.rmi.RemoteException;

import mochitoConnection.interfaces.client.MochitoClientConnection;
import mochitoConnection.interfaces.server.MochitoServerConnection;
import mochitoConnection.rmi.client.MochitoClientRMI;
import mochitoConnection.rmi.server.MochitoServerRuntime;
import mochitoTransferFactory.mochitoHost.MochitoHost;

/**
 * @author bin04017@gmail.com
 */
public class MochitoTransferFactory {
	/**
	 * host to connect or local host for server
	 */
	private MochitoHost host = null;
	
	private MochitoServerConnection serverConnection = null;
	
	private MochitoClientConnection clientConnection = null;

	private static MochitoTransferFactory instance = null;

	private MochitoServerRuntime runtime;

	protected MochitoTransferFactory(String host) throws MochitoException {
		this.host = new MochitoHost(host);
	}

	public MochitoTransferFactory(InetAddress localHost) throws MochitoException {
		this.host = new MochitoHost(localHost);
	}

	public static MochitoTransferFactory getInstance() throws MochitoException{
		if(instance == null){
			try {
				instance = new MochitoTransferFactory(InetAddress.getLocalHost());
			} catch (UnknownHostException e) {
				throw new MochitoException(e);
			}
		}
		return instance;
	}
	public static MochitoTransferFactory getInstance(String host) throws MochitoException {
		if (instance == null) {
			instance = new MochitoTransferFactory(host);
		}
		return instance;
	}
	/**
	 * Server. start the server and geht a connection to it
	 * @return the MochitoServerConnection
	 * @throws MochitoException 
	 * @throws RemoteException 
	 */
	public MochitoServerConnection getConnectionOfServer() throws MochitoException {
		if (serverConnection == null && runtime == null) {
			try {
				runtime = new MochitoServerRuntime(host);
				serverConnection = runtime.getServer();
				runtime.run();
			} catch (Exception e) {
				throw new MochitoException(e);
			}	
		} else if (runtime == null){
			serverConnection = runtime.getServer();
		}
		return serverConnection;
	}
	public MochitoClientConnection getConnectedToServer(String ipAsString) throws MochitoException {
		this.host = new MochitoHost(ipAsString);
		if (clientConnection == null) {
			try {
				clientConnection = new MochitoClientRMI(host);
			} catch (RemoteException e) {
				throw new MochitoException(e);
			}
		}
		return clientConnection;
	}
	/**
	 * Client. the connection to the local server
	 * @return the MochitoClientConnection
	 * @throws MochitoException 
	 */
	public MochitoClientConnection getConnectedToServer() throws MochitoException {
		if (clientConnection == null) {
			try {
				clientConnection = new MochitoClientRMI(host);
			} catch (RemoteException e) {
				throw new MochitoException(e);
			}
		}
		return clientConnection;
	}

	/**
	 * @return the host
	 */
	public MochitoHost getHost() {
		return host;
	}	
}
